<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="参数名" prop="paramKey">
        <el-input v-model="dataForm.paramKey" placeholder="参数名"></el-input>
      </el-form-item>
      <el-form-item label="参数类型" prop="type">
        <el-select style="width: 100%" clearable v-model="dataForm.type" >
          <el-option
            v-for="item in paramType"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="参数值" prop="paramValue">
        <el-input v-if="dataForm.type == 0" v-model="dataForm.paramValue" placeholder="参数值"></el-input>
        <el-button v-if="dataForm.type == 1"  icon="el-icon-plus" @click="onAddItem()"></el-button>
        <el-popover
          placement="top-start"
          title="添加映射值"
          width="408"
          trigger="manual"
          v-model="isShowMapping">
          <div class="time-content">
            <el-form :model="mappingForm" ref="mappingForm" size="mini" @keyup.enter.native="onSaveMapping()" label-width="128px">
              <el-form-item label="名称" prop="label">
                <el-input v-model="mappingForm.label" placeholder="映射值"></el-input>
              </el-form-item>
              <el-form-item label="值" prop="value">
                <el-input v-model="mappingForm.value" placeholder="映射字段"></el-input>
              </el-form-item>
            </el-form>
            <div class="mapping-bt">
              <el-button type="text" style="color: #cccccc;" @click="isShowMapping = false;">取消</el-button>
              <el-button type="text" @click="onSaveMapping">保存</el-button>
            </div>
          </div>
        </el-popover>
        <div class="mapping-list" v-if="dataForm.type == 1">
          <el-table :data="mappingList" border style="width: 100%;" size="mini">
            <el-table-column
              prop="label"
              header-align="center"
              align="center"
              label="名称">
            </el-table-column>
            <el-table-column
              prop="value"
              header-align="center"
              align="center"
              label="值">
            </el-table-column>
            <el-table-column
              fixed="right"
              header-align="center"
              align="center"
              width="90"
              label="操作">
              <template slot-scope="scope">
                <el-button  type="text" size="small" @click="onMappingDelete(scope.row.value)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      isShowMapping: false,
      mappingList:[],
      paramType:[{'label':'普通',value:0},{'label':'JSON',value:1}],
      mappingForm:{
        label:'',
        value:''
      },
      dataForm: {
        id: 0,
        paramKey: '',
        paramValue: '',
        type: 1,
        remark: ''
      },
      dataRule: {
        paramKey: [
          { required: true, message: '参数名不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '参数类型不能为空', trigger: 'blur' }
        ],
        paramValue: [
          { required: true, message: '参数值不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //添加映射值
    onAddItem(){
      this.isShowMapping = true;
      this.$nextTick(() => {
        this.$refs['mappingForm'].resetFields()
      });
    },
    //删除映射值
    onMappingDelete(id){
      this.mappingList = this.mappingList.filter(item => {return id != item.value});
      this.dataForm.paramValue = JSON.stringify(this.mappingList)
    },
    //保存映射值
    onSaveMapping(){
      const index = this.mappingList.length;
      this.mappingList.push({label:this.mappingForm.label,value:this.mappingForm.value});
      this.isShowMapping = false;
      this.dataForm.paramValue = JSON.stringify(this.mappingList)
    },
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/config/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.paramKey = data.config.paramKey
              this.dataForm.type = data.config.type
              this.dataForm.paramValue = data.config.paramValue
              if(this.dataForm.type == 1){
                this.mappingList = JSON.parse(data.config.paramValue)
              }
              this.dataForm.remark = data.config.remark
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sys/config/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'paramKey': this.dataForm.paramKey,
              'paramValue': this.dataForm.paramValue,
              'type': this.dataForm.type,
              'remark': this.dataForm.remark
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped>
.mapping-bt{
  position: absolute;
  top: 0px;
  right: 12px;
}
</style>
